

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import java.awt.Frame;
import java.awt.image.renderable.ParameterBlock;
import java.io.IOException;
import javax.media.jai.*;
import java.awt.image.*;
import java.awt.image.renderable.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;

import java.awt.Color;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.Vector;


/**
 applescript 
 tell application "Safari"
 activate
 get the bounds of the first window
 end tell
 \
 */

//osascript ./srcipt.txt
//'tell application "Safari" activate  get the bounds of the first window  end tell ' 

//CLASSPATH=/System/Library/Java/:./

//import javax.media.jai.RenderedOp;
public class findImage extends Thread {
		String board;
		String hand;
		String c1="",c2="";
		public  int[] card;
		public int xwindow=5;
		public int ywindow=30;
		public int xcadre = 16;
		public int ycadre = 18;
		public int  yfigure= 206;
		public int ycolor = 224;
		int mod=0;
		
	
	Runtime rt;

	findImage(){
	
		this.card = new int[5];
		
		rt = Runtime.getRuntime();
	}
	public void calcHood(){
		String s,cmde="";
		try {
			int flop = board.trim().length();
			System.out.println(flop);
			//if( (flop== 6 ) || (flop== 8 )  || (flop== 10 ) ) 
				cmde= (" ../poker-eval/examples/pokenum -h "+ c1 + " " + c2 + " XX"  + "  --  " + board);
			//else 
			 //cmde = (" ../poker-eval/examples/pokenum -h "+ c1 + " " + c2 + " XX" );
			 System.out.println(cmde);
			Process proc = rt.exec(cmde);
	                BufferedReader stdInput = new BufferedReader(new 
                        InputStreamReader(proc.getInputStream()));
						
			while ((s = stdInput.readLine()) != null) {
                     	  System.out.println(s);
                   		}
				proc.destroy();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
	}
	public void parse(String S){
	if (S.contains(":::")){
		StringTokenizer item = new StringTokenizer(S,":::");
			while (item.hasMoreElements()){
				String token = item.nextToken();
				token=token.replaceAll("10","T");
				token=token.replaceAll("11","J");
				token=token.replaceAll("12","Q");
				token=token.replaceAll("13","K");
				token=token.replaceAll("14","A");
				if (mod%2==0) {
					c1 = token;
					
				}
				else  {
					c2  = token;
					
				}
				mod++;
			}
		}
			
	}
	public void getHand(){
	 FileInputStream in;
	 String S="";
		  int c;
		  int i=0;
		  try {
			  //in = new FileInputStream(nomdossier+nomfichier);
			  in = new FileInputStream("/Users/richard/Library/Logs/PokerStars/PokerStars.log.0");
			  while ( (c = in.read()) != -1) {
				i ++;
				String aChar = new Character((char)c).toString();
				if ((char)c== '\n') { 
					parse(S.trim());	
					S = "";
				}
				else S = S + aChar	;


				
			}
			  
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public void ocr(){
		String s="";
		
		board ="";
		System.out.println("DEB");
		String cmds[]=new String[20];
	
		//cmds[0] = "/usr/local/bin/convert /tmp/3.bmp /tmp/3.ppm";
		cmds[0] = "/sw/bin/convert /tmp/F1.bmp /tmp/F1.ppm";
		cmds[1]="../gocr/bin/gocr -m 50 -p ../gocr/db/ /tmp/F1.ppm";
		cmds[2] = "/sw/bin/convert /tmp/C1.bmp /tmp/C1.ppm";
		cmds[3]="../gocr/bin/gocr -m 50 -p ../gocr/db/  /tmp/C1.ppm";
		cmds[4] = "/sw/bin/convert /tmp/F2.bmp /tmp/F2.ppm";
		cmds[5]="../gocr/bin/gocr -m 50 -p ../gocr/db/  /tmp/F2.ppm";
		cmds[6] = "/sw/bin/convert /tmp/C2.bmp /tmp/C2.ppm";
		cmds[7]="../gocr/bin/gocr -m 50 -p ../gocr/db/  /tmp/C2.ppm";	
		cmds[8] = "/sw/bin/convert /tmp/F3.bmp /tmp/F3.ppm";
		cmds[9]="../gocr/bin/gocr -m 50 -p ../gocr/db/  /tmp/F3.ppm";
		cmds[10] = "/sw/bin/convert /tmp/C3.bmp /tmp/C3.ppm";
		cmds[11]="../gocr/bin/gocr -m 50 -p ../gocr/db/  /tmp/C3.ppm";
		cmds[12] = "/sw/bin/convert /tmp/F4.bmp /tmp/F4.ppm";
		cmds[13]="../gocr/bin/gocr -m 50 -p ../gocr/db/  /tmp/F4.ppm";
		cmds[14] = "/sw/bin/convert /tmp/C4.bmp /tmp/C4.ppm";
		cmds[15]="../gocr/bin/gocr -m 50 -p ../gocr/db/  /tmp/C4.ppm";
				
		cmds[16] = "/sw/bin/convert /tmp/F5.bmp /tmp/F5.ppm";
		cmds[17]="../gocr/bin/gocr -m 50 -p ../gocr/db/  /tmp/F5.ppm";
		cmds[18] = "/sw/bin/convert /tmp/C5.bmp /tmp/C5.ppm";
		cmds[19]="../gocr/bin/gocr -m 50 -p ../gocr/db/  /tmp/C5.ppm";				
		Process proc=null ;
		for (int i =0 ; i < cmds.length;i++){
			
			String cmde = cmds[i];
			s="--";
		try {
			//String cmde = "/tmp/hud-poker/tools/gocr/bin/gocr /tmp/2.bmp";
			//Thread.sleep(1000);
			proc = rt.exec(cmde);
			int exitCode = proc.waitFor();
			BufferedReader stdInput = new BufferedReader(new 
														 InputStreamReader(proc.getInputStream()));
			
			while ((s = stdInput.readLine()) != null) {
				//System.out.println(s);
				if (s.contains("A") ) board = board + " A";
				if (s.contains("2") ) board = board + " 2";
				if (s.contains("3") ) board = board + " 3";
				if (s.contains("4") ) board = board + " 4";
				if (s.contains("5") ) board = board + " 5";
				if (s.contains("6") ) board = board + " 6";
				if (s.contains("7") ) board = board + " 7";
				if (s.contains("8") ) board = board + " 8";
				if (s.contains("9") ) board = board + " 9";
				if (s.contains("I") ) board = board + " T";
				if (s.contains("J") ) board = board + " J";
				if (s.contains("Q") ) board = board + " Q";
				if (s.contains("K") ) board = board + " K";
				
				if (s.contains("t") ) board = board + "s";
				if (s.contains("+") ) board = board + "d";
				if (s.contains("m") ) board = board + "c";
				if (s.contains("C") ) board = board + "h";
			}
				
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		proc.destroy();
		System.out.println(board);
		
			
	}
	public void calculatecoordinate(){
		/*this.card=this.pos;
		pos[2] = pos[2]-pos[0];
		pos[3] = pos[3]-pos[1];
		*/
		card[0]=274;
		card[1]=328;
		card[2]=382;
		card[3]=436;
		card[4]=490;
		
	}
	public void applecsript(String script){
		String s="";
	
		try {
		String cmde = "/usr/bin/osascript ./script.txt";
		Process proc = rt.exec(cmde );
		BufferedReader stdInput = new BufferedReader(new 
													 InputStreamReader(proc.getInputStream()));
		
			/*s = stdInput.readLine();
			int j=0;
				StringTokenizer item = new StringTokenizer(s,",");
			while (item.hasMoreElements()){
				String token = item.nextToken();
				 int  i=Integer.parseInt(token.trim());
				pos[j]=i;
				j++;
				System.out.println(i);
				
			}
		//	System.out.println(s);
			*/
			int exitCode = proc.waitFor();
		
			proc.destroy();
	} catch (Exception e) {
			// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
		
		
	}
	
	

	
	        public static void main(String[] args) throws InterruptedException{
				findImage fHand  = new findImage();
				 fHand.run();
		}
		public void run(){
			
        	BufferedImage bi=null;
			calculatecoordinate();	
		Robot robot=null;
		try {
		robot = new Robot();
		} catch (AWTException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}
		
				while(true){
				applecsript("");
				try{
				
				 Thread.sleep(200);
			
				
				bi=robot.createScreenCapture(new Rectangle(1024,768));
				// ImageIO.write(bi, "BMP", new File("/tmp/1.bmp"));
				/*ImageIO.write(bi.getSubimage( card[0],  card[1], cadre[0], cadre[1]),"bmp", new File("/tmp/1.bmp"));
				ImageIO.write(bi.getSubimage( card[0],  card[1], card[2]/2, card[3]/2),"bmp", new File("/tmp/2.bmp"));
				ImageIO.write(bi.getSubimage( card[0],  card[1], card[2]/3, card[3]/3),"bmp", new File("/tmp/3.bmp"));
				ImageIO.write(bi.getSubimage( card[0],  card[1], card[2]/3, card[3]/3),"bmp", new File("/tmp/4.bmp"));
				*/
				ImageIO.write(bi.getSubimage( card[0],  yfigure, xcadre, ycadre),"bmp", new File("/tmp/F1.bmp"));	
				ImageIO.write(bi.getSubimage( card[0],  ycolor, xcadre, ycadre),"bmp", new File("/tmp/C1.bmp"));
				ImageIO.write(bi.getSubimage( card[1],  yfigure, xcadre, ycadre),"bmp", new File("/tmp/F2.bmp"));
				ImageIO.write(bi.getSubimage( card[1],  ycolor, xcadre, ycadre),"bmp", new File("/tmp/C2.bmp"));
				ImageIO.write(bi.getSubimage( card[2],  yfigure, xcadre, ycadre),"bmp", new File("/tmp/F3.bmp"));
				ImageIO.write(bi.getSubimage( card[2],  ycolor, xcadre, ycadre),"bmp", new File("/tmp/C3.bmp"));
				ImageIO.write(bi.getSubimage( card[3],  yfigure, xcadre, ycadre),"bmp", new File("/tmp/F4.bmp"));
				ImageIO.write(bi.getSubimage( card[3],  ycolor, xcadre, ycadre),"bmp", new File("/tmp/C4.bmp"));
				ImageIO.write(bi.getSubimage( card[4],  yfigure, xcadre, ycadre),"bmp", new File("/tmp/F5.bmp"));
				ImageIO.write(bi.getSubimage( card[4],  ycolor, xcadre, ycadre),"bmp", new File("/tmp/C5.bmp"));
			} 
            // Capture the screen shot of the area of the screen defined by the rectangle
			catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 catch (InterruptedException e) {
			 	e.printStackTrace();
			}
           
			   // Thread.sleep(1000);
				 ocr();
				 getHand();
				System.out.println ( c1 + " " +  c2);
				 calcHood();
				
			}
		}
}

